tongfusoft.metadata = function() {
}
tongfusoft.metadata.prototype = {

	initTable : function() {

		var table = new tongfusoft.table();
		var columns = [
						{
							"data" : "metadataId"
						},
						{
							"data" : "metadataId",
							"render" : function(data, type, full, meta) {
								var metadataId = '<input type="hidden" class="metadataIdData" name="metadataIdData" value="'
										+ data + '" />'
								return metadataId;
							}
						},
						{
							"data" : "metadataTag"
						},
						{
							"data" : "metadataName"
						},
						{
							"data" : "dictkeyName",
							
						},
						{
							"data" : "metadataNote"
						},
						{
							"data" : "loginName"
						},
						{
							"data" : "lastUpdDate"
						},
						{
							"data" : "isEnable",
							"render" : function(data, type, full, meta) {
								if (data == '1') {
									var isEnable = '<label class="switch ">'
											+ '<input type="checkbox" checked="checked" onclick="new tongfusoft.metadata().switchIsEnable(this)">'
											+ '<span></span>'
											+ '</label>'
											+ '<input type="hidden" class="isEnable" name="isEnable" value="'
											+ data + '" />'
									return isEnable;
								} else {
									var isEnable = '<label class="switch ">'
											+ '<input type="checkbox" onclick="new tongfusoft.metadata().switchIsEnable(this)">'
											+ '<span></span>'
											+ '</label>'
											+ '<input type="hidden" class="isEnable" name="isEnable" value="'
											+ data + '" />'
									return isEnable;
								}
							}
						},
						{
							"data" : "isEnable",
							"render" : function(data, type, full, meta) {
								if (data == '0') {
									var detailbut = '<button metadataId="'
										+ data
										+ '" onclick="new tongfusoft.metadata().detail(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="icon-list"></em></div></button>'
									var delbut = '<button metadataId="'
											+ data
											+ '" onclick="new tongfusoft.metadata().del(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
									var editbut = '<button metadataId="'
											+ data
											+ '" onclick="new tongfusoft.metadata().editPage(this)" type="button" data-toggle="modal" class="ecitBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

									return detailbut + editbut + delbut;
								} else {
									var detailbut = '<button metadataId="'
										+ data
										+ '" onclick="new tongfusoft.metadata().detail(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="icon-list"></em></div></button>'
									var hidden = '<input type="hidden"  value="' + data + '" />'
									var delbut = '<button metadataId="'
										+ data
										+ '" onclick="new tongfusoft.metadata().del(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
								    var editbut = '<button metadataId="'
										+ data
										+ '" onclick="new tongfusoft.metadata().editPage(this)" type="button" data-toggle="modal" class="ecitBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

										return detailbut + editbut + delbut;
								}
							}
						}]

				table.dataTable('metadataTable', 'metadata/query', columns, null);


			},

	createPage : function() {
		$("#addBtn").click(function() {
			
		    window.location.href=common.getContextPath('manager/metadata/addMetadata');
		})

	},
	
	detail : function(obj) {
		var metadataId = $(obj).parents("tr").find(".metadataIdData").val();
		
		  window.location.href=common.getContextPath('manager/metadata/metadataDetail/' + metadataId);

	 },
	
	//删除
	del : function(obj) {
		var metadataId = $(obj).parents("tr").find(".metadataIdData").val();
		swal({
			title : "是否确认?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#DD6B55",
			confirmButtonText : "删除",
			cancelButtonText : "取消",
			closeOnConfirm : true
		}, function() {
			common.ajaxSubmitOnSuccess({
				"metadataId" : metadataId
			}, 'metadata/del', function(data) {

				if (data.state == 'success') {

					$('#metadataTable').DataTable().ajax.reload();
				}else{
					toastr["error"]('删除失败!');
				}

			}, "del")
		});

	},
	
	editPage : function(obj) {
		var metadataId = $(obj).parents("tr").find(".metadataIdData").val();
		window.location.href=common.getContextPath('manager/metadata/updateMetadata/' + metadataId);

	},
	// 启用模板
	switchIsEnable : function(obj) {
		var metadataId = $(obj).parents("tr").find(".metadataIdData").val();
		var hasIsEnable = $(obj).parents("tr").find(".isEnable").val();
		var isEnable = "";
		if (hasIsEnable === '0') {
			isEnable = '1';
		} else {
			isEnable = '0';
		}
		common.ajaxSubmitOnSuccess({
			"metadataId" : metadataId,
			"isEnable" : isEnable
		}, 'metadata/isEnable', function(data) {

			if (data.state == 'success') {
				toastr["success"]('切换成功!');
				$('#metadataTable').DataTable().ajax.reload();
			}else{
				toastr["error"]('切换失败!');
			}
		})

	},
	search : function() {

		$("#searchBtn").click(function() {

			$('#metadataTable').DataTable().ajax.reload();

		})

	}
}

$(function() {
 var obj = new tongfusoft.metadata();
 obj.initTable();
 obj.createPage();
 obj.search();
 obj.dataSubmit();

})